<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalBasePage.php 296 2008-11-16 17:42:15Z steen.rabol@gmail.com $
 * @package Pradoportal.Common
 *
 */

/**
 *
 * @package Pradoportal.Common
 */

Prado::using('Application.Common.Data.PortalPageRecord');

abstract class PortalBasePage extends TPage
{
	private $_editmode			= 0;
	private $_minRole			= 0;
	private $_pageRow			= null;
	private $_permissions		= null;
	private $_requestedpagepath	= "";
	private	$_canedit			= false;
	private $_canview			= false;
	private	$_langstrings		= array();

	public function onPreInit($param)
	{
		parent::onPreInit($param);

		if($this->Application->Parameters['RunMode'] == 'Install' && $this->Service->RequestedPagePath != "Install.Install")
		{
			$this->gotoPage("Install.Install");
		}

		if($this->Application->Parameters['RunMode'] != 'Install')
		{
			$cookieculture		= PortalUtil::getCookie($this->Application->Parameters['CookiePrefix'] . "_lang");
			if($cookieculture)
			{
				$culture = $cookieculture->Value;
			}
			else
			{
				$culture = $this->Application->Parameters['DefaultLanguage'];
			}

			$this->Application->Culture = $culture;
			$this->Application->loadLanguageStrings();

			// Set admin email address
			if(!isset($_SERVER['SERVER_ADMIN']) || empty($_SERVER['SERVER_ADMIN']))
			{
				$_SERVER['SERVER_ADMIN'] = $this->Application->Parameters['AdminEmail'];
			}

			$this->MinRole	= $this->Application->Parameters['MinRole'];
			$this->PageRow	= PortalPageRecord::finder()->findByfullpath($this->Service->RequestedPagePath);

			// check the login expire cookie to see if we need to 'kick' out the current user
			if(TPropertyValue::ensureBoolean($this->Application->Parameters['UseCookieExpire']))
			{
				$cookie = PortalUtil::getCookie($this->Application->Parameters['CookiePrefix'] . "_loginexpire");

				if($cookie !== null)
				{
					if(!$this->User->IsGuest) // Only check logged in users
					{
						$check_time = time();
						if($cookie->Value < $check_time)
						{
							$this->Application->getModule('auth')->logout();
							// The cookie is cleared in the In active page to prevent endless loop
							$this->gotoPage('System.InActiveLogin');
						}
					}
				}
				else
				{
					$cookie			= new THttpCookie($this->Application->Parameters['CookiePrefix'] . "_loginexpire",0);
				}

				// Upate the cookie
				$cvalue			= TPropertyValue::ensureInteger((time() + ($this->Application->Parameters['LoginExpire'] * 60)));
				$cookie->value	= $cvalue;
				$cookie->Expire	= $cvalue + 3600;
				$this->Response->addCookie($cookie);
			}
		}
	}

	public function onInit($param)
	{
		parent::onInit($param);

		if($this->Application->Parameters['RunMode'] != 'Install' && $this->Service->RequestedPagePath != "Install.Install")
		{
			$am				= $this->Application->getModule('auth');
			$this->CanEdit	= $am->CanEdit($this->Service->RequestedPagePath);
			$this->CanView	= $am->CanView($this->Service->RequestedPagePath);

			if(TPropertyValue::ensureInteger($this->Request['PageMode']) == 1 && $this->CanEdit)
			{
				$this->EditMode = 1;
			}
			else
			{
				$this->EditMode	= 0;
			}

			if(!$this->CanView)
			{
				$this->reportError(1,Prado::localize("You do not have permission to view this page"));
			}
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$prTitle	= "";
		if($this->PageRow)
		{
			$prTitle	= $this->PageRow->displayname;
		}
		else
		{
			$prTitle	= $this->Service->RequestedPagePath;
		}

		$this->Title	= sprintf("%s - %s", $this->Application->Parameters['SiteTitle'],$prTitle);
	}

	abstract function gotoDefaultPage();

	public function gotoPage($pagePath,$getParameters = null)
	{
		$this->Response->redirect($this->Service->constructUrl($pagePath,$getParameters));
	}

	public function reportError($errorCode, $errorMsg)
	{
		$this->gotoPage('System.ErrorReport',array('id'=>$errorCode,'msg' => urldecode($this->getApplication()->getSecurityManager()->hashData($errorMsg))));
	}

	/*
	 * Class properties
	 */

	public function getDataAccess()
	{
		return $this->getApplication()->DataAccess;
	}

	public function getDbConnection()
	{
		return $this->getApplication()->DbConnection;
	}

	public function getRequestedPagePath()
	{
		return $this->_requestedpagepath;
	}

	public function setRequestedPagePath($value)
	{
		$this->_requestedpagepath = $value;
	}

	public function getCanEdit()
	{
		return $this->_canedit;
	}

	public function setCanEdit($value)
	{
		$this->_canedit	= $value;
	}

	public function getCanView()
	{
		return $this->_canview;
	}

	public function setCanView($value)
	{
		$this->_canview	= $value;
	}

	public function getEditMode()
	{
		return $this->_editmode;
	}

	public function setEditMode($value)
	{
		$this->_editmode = $value;
	}

	public function setMinRole($value)
	{
		$this->_minRole = $value;
	}

	public function getMinRole()
	{
		return $this->_minRole;
	}

	public function getPageRow()
	{
		return $this->_pageRow;
	}

	public function setPageRow($value)
	{
		$this->_pageRow = $value;
	}

	public function getPermissions()
	{
		return $this->_permissions;
	}

	public function setPermissions($value)
	{
		$this->_permissions	= TPropertyValue::ensureArray($value);
	}

	public function setPageTitle($pageName = null)
	{
		$this->Title =  $this->Application->Parameters['SiteTitle'].' ' . (($pageName !== null)? ' - ' . Prado::localize($pageName) : '');
	}
}
?>